package Net.tcp.loagin;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 模拟登陆：多次访问
 * 创建服务器
 * 1.指定端口 使用ServerSocket创建服务器
 * 2.阻塞式等待链接accept
 * 3.操作：输入输出流操作
 * 4.释放资源
 */
public class LoginMoreServer {
    public static void main(String[] args) throws IOException {
        System.out.println("----Server----");
        //1.指定端口 使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        boolean isRunning = true;

        while (isRunning){
            // 2.阻塞式等待链接accept
            Socket client = server.accept();
            System.out.println("客户端建立了一个连接");
            new Thread(new Channel(client)).start();
        }
        server.close();
    }

    static class Channel implements Runnable{
        private Socket client;
        //输入流封装
        private DataInputStream dis;
        //输出
        private DataOutputStream dos;
        public Channel(Socket client){
            this.client=client;
            try {
                dis = new DataInputStream(client.getInputStream());
                //输出
                dos = new DataOutputStream(client.getOutputStream());//getOutputStream返回此套接字的输出流。
            } catch (IOException e) {
                e.printStackTrace();
                release();
            }
        }
        @Override
        public void run() {
            // 3.操作：输入输出流操作
            String user="";
            String password="";
            //分析数据
            String[] dataArray = raceive().split("&");
            for (String info:dataArray){
                String[] userInfo = info.split("=");
                if(userInfo[0].equals("uname")){
                    System.out.println("用户名为---->"+userInfo[1]);
                    user = userInfo[1];
                }else if(userInfo[0].equals("password")){
                    System.out.println("密码为----->"+userInfo[1]);
                    password = userInfo[1];
                }
            }

            //模拟成功失败
            if(user.equals("zhangzhe") && password.equals("zz")){//成功
               send("登录成功");
            }else{
                send("用户名或密码错误");
            }
            release();
        }
        //接收数据
        private String raceive(){
            String datas="";
            try {
                datas = dis.readUTF();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return datas;
        }
        //发送数据
        private void send(String msg){
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 4.释放资源
        private void release(){
            try {
                if(null!=dis){
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(null!=dos) {
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(null!=client) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
